package com.itkaien.javaeebased.demo01;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class GameJFrame extends JFrame implements KeyListener , ActionListener{


    int step=0;
    int x = 0;
    int y = 0;
    String path = "image/animal/animal3/";

    int [][]win = new int[][]{
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    JMenuItem restartGame = new JMenuItem("重新开始游戏");
    JMenuItem reLogin = new JMenuItem("重新登录");
    JMenuItem closeGame = new JMenuItem("关闭游戏");
    JMenu updatePhotoMenu = new JMenu("更换图片");
    JMenuItem accountItem = new JMenuItem("公众号");

    JMenuItem girlItem = new JMenuItem("美女");
    JMenuItem animalItem = new JMenuItem("动物");
    JMenuItem sportItem = new JMenuItem("运动");

    //JFrame
    public GameJFrame() {

        initFrame();
        initMenuBar();
        initData();
        initImage();
        //设置窗口的可见性
        this.setVisible(true);
    }
    public void initFrame() {
        //设置窗口的标题
        this.setTitle("2048拼图小游戏");
        //至于图层最上测
        this.setAlwaysOnTop(true);
        //设置窗口的宽和高
        this.setSize(603, 680);
        //设置窗口的关闭按钮的默认操作(X)
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗口的布局 取消默认的居中放置
        this.setLayout(null);
        //居中
        this.setLocationRelativeTo(null);

        this.addKeyListener(this);
    }

    public void initMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu functionMenu = new JMenu("功能");
        JMenu abboutMenu = new JMenu("关于我们");
        functionMenu.add(restartGame);
        functionMenu.add(reLogin);
        functionMenu.add(closeGame);
        functionMenu.add(updatePhotoMenu);
        abboutMenu.add(accountItem);
        menuBar.add(functionMenu);
        menuBar.add(abboutMenu);


        updatePhotoMenu.add(girlItem);
        updatePhotoMenu.add(animalItem);
        updatePhotoMenu.add(sportItem);


        //给条目绑定事件
        restartGame.addActionListener(this);
        reLogin.addActionListener(this);
        closeGame.addActionListener(this);
        updatePhotoMenu.addActionListener(this);
        accountItem.addActionListener(this);
        girlItem.addActionListener(this);
        animalItem.addActionListener(this);
        sportItem.addActionListener(this);

        this.setJMenuBar(menuBar);
    }

    int[][] data = new int[4][4];

    public void initData() {
        int[] temp = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

        Random random = new Random();
        for (int i = 0; i < temp.length; i++) {
            int index = random.nextInt(temp.length);
            int tempValue = temp[i];
            temp[i] = temp[index];
            temp[index] = tempValue;
        }
        for (int i = 0; i < temp.length; i++) {
            if (temp[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
                data[i / 4][i % 4] = temp[i];
        }

    }

    private void initImage() {
        this.getContentPane().removeAll();

        if (victory()){
            JLabel winJlable=new JLabel(new ImageIcon("image/win.png"));
            winJlable.setBounds(203,283,197,73);
            this.getContentPane().add(winJlable);
        }


        JLabel stepCount = new JLabel("步骤"+step);
        stepCount.setBounds(50,30,100,20);
        this.getContentPane().add(stepCount);

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int num = data[i][j];
                ImageIcon icon = new ImageIcon(path + num + ".jpg");
                JLabel label = new JLabel(icon);
                label.setBounds(j * 105 + 83, i * 105 + 134, 105, 105);
                label.setBorder(new BevelBorder(BevelBorder.LOWERED));
                this.getContentPane().add(label);
            }
        }
        ImageIcon iconback = new ImageIcon("image/background.png");
        JLabel backgroundLabel = new JLabel(iconback);
        backgroundLabel.setBounds(40, 40, 508, 560);
        this.add(backgroundLabel);
        this.getContentPane().repaint();
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }
    //按下不松开的时候调用这个方法
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == 65) {
            this.getContentPane().removeAll();
            JLabel label = new JLabel(new ImageIcon(path + "all.jpg"));
            label.setBounds(83, 134, 420, 420);
            this.getContentPane().add(label);
            ImageIcon iconback = new ImageIcon("image/background.png");
            JLabel backgroundLabel = new JLabel(iconback);
            backgroundLabel.setBounds(40, 40, 508, 560);
            this.add(backgroundLabel);
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

        if (victory()){
            return;
        }

        try {
            int i = e.getKeyCode();
            if (i == 38) {
                for (int p = 0; p < data.length; p++) {
                    for (int q = 0; q < data[p].length; q++) {
                        System.out.print(data[p][q] + "\t");
                    }
                    System.out.println();
                }
                System.out.println("上");
                data[x][y] = data[x + 1][y];
                data[x + 1][y] = 0;
                x++;
                step++;
                System.out.println(x);
                initImage();
            } else if (i == 39) {
                System.out.println("右");
                for (int p = 0; p < data.length; p++) {
                    for (int q = 0; q < data[p].length; q++) {
                        System.out.print(data[p][q] + "\t");
                    }
                    System.out.println();
                }
                data[x][y] = data[x][y - 1];
                data[x][y - 1] = 0;
                y--;
                step++;
                initImage();
            } else if (i == 40) {
                System.out.println("下");
                for (int p = 0; p < data.length; p++) {
                    for (int q = 0; q < data[p].length; q++) {
                        System.out.print(data[p][q] + "\t");
                    }
                    System.out.println();
                }
                data[x][y] = data[x - 1][y];
                data[x - 1][y] = 0;
                x--;
                step++;
                initImage();
            } else if (i == 37) {
                for (int p = 0; p < data.length; p++) {
                    for (int q = 0; q < data[p].length; q++) {
                        System.out.print(data[p][q] + "\t");
                    }
                    System.out.println();
                }
                System.out.println("左");
                data[x][y] = data[x][y + 1];
                data[x][y + 1] = 0;
                y++;
                step++;
                initImage();
            } else if (i == 65) {
                initImage();
            } else if (i == 87) {
                data = new int[][]{
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                        {13, 14, 15, 0}
                };
                initImage();
            }
        } catch (Exception exception) {
            if (exception instanceof ArrayIndexOutOfBoundsException) {
                System.out.println("边界");
            } else {
                System.out.println("其他异常");
            }
        }
    }
    public boolean victory() {
        for (int p = 0; p < data.length; p++) {
            for (int q = 0; q < data[p].length; q++) {
               if (data[p][q] != win[p][q]) {
                   return false;
               }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == restartGame) {
            step=0;
            initData();
            initImage();
            System.out.println("重新开始");
        }else if (source==reLogin){
            System.out.println("重新登录");
            this.setVisible(false);
            new LoginJFrame();
        }else if (source==closeGame){
            System.out.println("关闭游戏");
            System.exit(0);
        } else if (source==accountItem) {
            System.out.println("关于我们");
//            this.setVisible(false);
            JDialog jDialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("image/about.png"));
            jLabel.setBounds(0, 0, 258, 258);
            jDialog.setSize(344,344);

            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);

            jDialog.add(jLabel);
            jDialog.setVisible(true);
        }else if(source==updatePhotoMenu){
            System.out.println("updatePhotoMenu");
        } else if (source==girlItem) {
            System.out.println("girlItem");
            Random random = new Random();
            int order = random.nextInt(13)+1;
            System.out.println("order:"+order);
            path = "image/girl/girl"+order+"/";
            initData();
            initImage();
        } else if (source==animalItem) {
            System.out.println("animalItem");
            Random random = new Random();
            int order = random.nextInt(8)+1;
            System.out.println("order:"+order);
            path = "image/animal/animal"+order+"/";
            initData();
            initImage();
        } else if (source==sportItem) {
            System.out.println("sportItem");
            Random random = new Random();
            int order = random.nextInt(10)+1;
            System.out.println("order:"+order);
            path = "image/sport/sport"+order+"/";
            initData();
            initImage();
        }
    }
}
